package com.credithc.customers.base.service.impl;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.convert.BaseConverter;
import com.credithc.baseserv.core.exception.BsException;
import com.credithc.baseserv.core.service.impl.BaseServiceImpl;
import com.credithc.baseserv.core.utils.DateTimeUtils;
import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.customers.base.codes.CustomerCodes;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.base.mapper.SystemDictMapper;
import com.credithc.customers.base.po.SystemDictPo;
import com.credithc.customers.base.ro.SystemDictRo;
import com.credithc.customers.base.service.ISystemDictionaryService;
import com.credithc.customers.base.vo.SystemDictVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title:
 * @Author: Denghx
 * @Date: 2018/12/11 15:18
 * @Description: com.credithc.customers.manager.service.impl
 * @Version: V1.0.0
 */
@Service

public class SystemDictionaryServiceImpl extends BaseServiceImpl<SystemDictPo, SystemDictRo, SystemDictVo> implements ISystemDictionaryService {
    @Autowired
    BaseConverter<SystemDictPo, SystemDictVo> converterDictionaryPoToVo;

    @Autowired
    BaseConverter<SystemDictRo, SystemDictPo> converterDictionaryRoToPo;

    @Autowired
    SystemDictMapper systemDictMapper;

    @Override
    public PageInfo<SystemDictVo> selectAll(SystemDictRo dictonary) {
        try {
            Example example = new Example(SystemDictPo.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("create_time desc");
            if (StringUtils.isNotEmpty(dictonary.getCode())) {
                criteria.andEqualTo("code", dictonary.getCode());
            }
            if (StringUtils.isNotEmpty(dictonary.getType())) {
                criteria.andEqualTo("type", dictonary.getType());
            }
            if (StringUtils.isNotEmpty(dictonary.getName())) {
                criteria.andEqualTo("name", dictonary.getName());
            }
            if (StringUtils.isNotEmpty(dictonary.getSts())) {
                criteria.andEqualTo("sts", dictonary.getSts());
            }
            PageInfo<SystemDictPo> dicPoPageInfo = this.queryPageResultByExample(example, dictonary.getPageNo(), dictonary.getPageSize());
            List<SystemDictPo> dictionaryPos = dicPoPageInfo.getList();
            List<SystemDictVo> dictionaryVos = buildParams(dictionaryPos);
            return new PageInfo<>(dictionaryVos, dicPoPageInfo.getTotalCount(), dictonary.getPageNo(), dictonary.getPageSize());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public void insert(SystemDictRo dictionary) {
        if (StringUtils.isEmpty(dictionary.getCode()) || StringUtils.isEmpty(dictionary.getName()) || StringUtils.isEmpty(dictionary.getType())) {
            throw new CustomerException(CustomerCodes.PARAM_VALIDATE_ERROR, "必填项不能为空");
        }
        try {
            SystemDictPo dictionaryPo = converterDictionaryRoToPo.convert(dictionary, SystemDictPo.class);
            dictionaryPo.setCreateTime(new Timestamp(System.currentTimeMillis()));
            systemDictMapper.insert(dictionaryPo);
        } catch (Exception e) {
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public void updateDictionary(SystemDictRo dictonary, Integer id) {

        try {
            SystemDictPo dictionaryPo = converterDictionaryRoToPo.convert(dictonary, SystemDictPo.class);
            dictionaryPo.setId(id);
            dictionaryPo.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            this.updateByPrimaryKeySelective(dictionaryPo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public void updateDictonarySts(Integer id) {

    }


    @Override
    public void remove(Integer id) {
        try {
            systemDictMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public Integer removeBatch(List<Integer> list) {
        try {
            Example example = new Example(SystemDictPo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", list);
            int result = systemDictMapper.deleteByExample(example);
            if (result == 0) {
                throw new BsException(CoreCodes.CORE_SYS_ERROR, "批量删除失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public List<SystemDictVo> selectByType(String type) {

        try {
            Example example = new Example(SystemDictPo.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotEmpty(type)) {
                criteria.andEqualTo("type", type);
            }
            List<SystemDictPo> dictionaryPos = systemDictMapper.selectByExample(example);
            List<SystemDictVo> smsDictionaryVos = converterDictionaryPoToVo.convertList(dictionaryPos, SystemDictVo.class);
            return smsDictionaryVos;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    @Override
    public PageInfo<SystemDictPo> getMapperType(String type) {
        try {
            Example example = new Example(SystemDictPo.class);
            Example.Criteria criteria = example.createCriteria();
            if(StringUtils.isNotBlank(type)){
                criteria.andEqualTo("type", type);
            }
            PageInfo<SystemDictPo> poPageInfo = this.queryPageResultByExample(example, 1, 9999);
            List<SystemDictPo> list = poPageInfo.getList();
            for(int i = 0 ; i < list.size(); i++){
                SystemDictPo systemDictPo = list.get(i);
                systemDictPo.setCode(systemDictPo.getType() + "_" +systemDictPo.getCode());
                systemDictPo.setName(systemDictPo.getType() + "_" +systemDictPo.getName());
            }
            return poPageInfo;
        } catch (Exception e) {
            throw new BsException(CoreCodes.CORE_SYS_ERROR);
        }
    }

    /**
     * 转换参数
     */
    public List<SystemDictVo> buildParams(List<SystemDictPo> pos) {
        List<SystemDictVo> dictionaryVos = new ArrayList<>();
        for (SystemDictPo info : pos) {
            SystemDictVo infoVO = new SystemDictVo();
            BeanUtils.copyProperties(info, infoVO);
            if (info.getCreateTime() != null) {
                infoVO.setCreateTime(DateTimeUtils.dateToStrLong(info.getCreateTime()));
            }
            if (info.getUpdateTime() != null) {
                infoVO.setUpdateTime(DateTimeUtils.dateToStrLong(info.getUpdateTime()));
            }
            dictionaryVos.add(infoVO);
        }
        return dictionaryVos;
    }
}
